/**
  ETFAna project, Anyang Normal University && IMP-CAS
  \class ETFVisual
  \brief event (2D) visualization
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-11-13
  \date 2021-12-11 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2021-11-16   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021-2024 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

// #include <TGeoManager.h>
// #include <TGeoMaterial.h>
// #include <TGeoMatrix.h>
// #include <TGeoMedium.h>
#include <TClonesArray.h>
#include <TCanvas.h>
#include <TGraph.h>
#include <TFile.h>
#include "ETFRun.h"
#include "ETFPID.h"
#include "ETFVisual.h"
#include "ETFAnalyzer.h"
#include "ETFDetectorManager.h"
#include "ETFTrackManager.h"
#include "ETFPIDManager.h"

ETFVisual *ETFVisual::fInstance = nullptr;
// for usual RIB experiments //
// double ETFVisual::kL = 10000.;
// double ETFVisual::kW = 3200.;

// #define _TA_LOCAL_ // define this if you want the view of only the target zone
// excluisvely for pion experiments
double ETFVisual::kL = 12000.;
double ETFVisual::kW = 3000.;

ETFVisual::ETFVisual() : fHasConfigured(false), fAnaLevel(0), fCanvas(0),
fRootFile(0), fGMainFrame(0){
  // ConfigureGeom();
} // end ctor

ETFVisual::~ETFVisual(){
  if(fCanvas) delete fCanvas;
  if(fGMainFrame) delete fGMainFrame;
} // end dtor

ETFVisual *ETFVisual::Instance(){
  if(!fInstance) fInstance = new ETFVisual();
  return fInstance;
} // end member function Instance

// void ETFVisual::AddNode(TGeoVolume *vol, int copy_no, TGeoMatrix *mat){
//   fTop->AddNode(vol, copy_no, mat);
// } // end member function AddNode

// void ETFVisual::ConfigureGeom(){
//   fGeom = new TGeoManager("ETFVisual", "ETF visualization");
//   TGeoMaterial *matVacuum = new TGeoMaterial("vacuum", 0., 0., 0.);
//   TGeoMedium *vacuum = new TGeoMedium("vacuum", 1, matVacuum);
//   // x: -1000., 3200., y: -600., 600., z: -2500., 8500.
//   fTop = fGeom->MakeBox("TOP", vacuum, 2100., 600., 5500.);
//   fGeom->SetTopVolume(fTop);
// } // end member function ConfigureGeom

void ETFVisual::Configure(){
  ETFRun *run = ETFRun::Instance();
  fRootFile = run->GetRootFile();
  fAnaLevel = run->GetAnalyzer()->AnaLevel();
  if(!fRootFile->FindObjectAny("VISUAL")) fRootFile->mkdir("VISUAL");
  fCanvas = new TCanvas("ETFAna-VISUAL", "ETFAna-VISUAL", 800, 600);

  fGMainFrame = new TGraph();
  fGMainFrame->SetNameTitle("MainFrame", "Experiment Snap");
  // Experiment visualization canvas size
  // (-W, W)       0                 (L, W)
  //
  // (-W,-W) 	    0                 (L,-W)
#ifdef _TA_LOCAL_
	fGMainFrame->SetPoint(0, -2700.,  80.);   fGMainFrame->SetPoint(1, -900., 80.);
	fGMainFrame->SetPoint(3, -2700., -80.); fGMainFrame->SetPoint(2,  -900, -80.);
  fGMainFrame->SetPoint(4, -2700,  80.); // this point seals the box
#else
	fGMainFrame->SetPoint(0, -kW,  kW);       fGMainFrame->SetPoint(1,  kL,  kW);
	// fGMainFrame->SetPoint(3, -kW, -kW+2000.); fGMainFrame->SetPoint(2,  kL, -kW+2000.);
	fGMainFrame->SetPoint(3, -kW, -kW); fGMainFrame->SetPoint(2,  kL, -kW);
  fGMainFrame->SetPoint(4, -kW,  kW); // this point seals the box
#endif
} // end member function Configure

// \param index: event index
void ETFVisual::DrawEventSnap(int index){
  if(!fHasConfigured){
    fHasConfigured = true;
    Configure();
  } // end if
  fCanvas->Draw();
  char name[64], title[128];
  sprintf(name, "Snap#%d", index);
  sprintf(title, "Experiment Snap - Event#%d", index);
  fCanvas->SetName(name); fCanvas->SetTitle(title);
  fGMainFrame->SetTitle(title);
  fGMainFrame->Draw("APL");
  // draw all the detecting units with highlighted hits
  ETFDetectorManager::Instance()->Draw(); // draw the det hits
  if(fAnaLevel >= 1) ETFTrackManager::Instance()->Draw(); // draw the tracks
  // draw the arc (trajectory) in the dipole magnet
  if(fAnaLevel >= 2) ETFPIDManager::Instance()->Draw();

  fRootFile->cd("VISUAL");
  fCanvas->Write("", TObject::kOverwrite);
  fCanvas->Close();
  fRootFile->cd(); // exit to the root folder
} // end member function DrawEventSnap
